package com.colorlight.cloud.complete.entity.event;

import com.colorlight.cloud.complete.api.CompleteEntityEvent;
import com.colorlight.cloud.complete.entity.po.RestoreResult;
import com.colorlight.iot.proto.complete.event.CompleteEvent;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 发送卡恢复出厂上报事件
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SetFactoryRestoreFinish implements CompleteEntityEvent {

    /**
     * 恢复结果
     */
    private List<RestoreResult> result;

    @Override
    public SetFactoryRestoreFinish byteConvertObject(ByteString bytes) {
        CompleteEvent.SetFactoryRestoreFinish setFactoryRestoreFinish = null;
        try {
            setFactoryRestoreFinish = CompleteEvent.SetFactoryRestoreFinish.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        Objects.requireNonNull(setFactoryRestoreFinish);
        List<RestoreResult> collect = setFactoryRestoreFinish.getResultList().stream()
                .map(e -> new RestoreResult().formProto(e)).collect(Collectors.toList());
        this.setResult(collect);
        return this;
    }
}

